<link rel="stylesheet" href="../public/css/style.css" type="text/css"/>
<?php

 require_once '../model/db.php';

class Pagination extends Db {

    public function Display($page = null, $totalWorks = null, $request = null, $file = null) {
        $display = 3;
        $total_pages = $totalWorks;
        $targetpage = $file;  
        $limit = PER_PAGE;         
        $page = $page;
        if ($page)
            $start = ($page - 1) * $limit;    
        else
            $start = 0;        

        if ($page == 0)
            $page = 1;     
        $prev = $page - 1;       
        $next = $page + 1;       
        $lastpage = ceil($total_pages / $limit);     
//render pagination
        $pagination = "";
        if ($lastpage > 1) {
            $pagination .= "<div id=\"paging\">";
//previous button && first button
            if ($page > 1){
                $pagination.= "<a href=\"$targetpage?page=1\">First</a>";
                $pagination.= "<a href=\"$targetpage?page=$prev\"><< previous</a>";
            }
            else{
                $pagination.= "<a href=\"$targetpage?page=1\">First</a>";
                $pagination.= "<span class=\"disabled\"><< previous</span>";
            }   
//pages     
            if ($lastpage < 7 + ($display * 2)) { //not enough pages to bother breaking it up
                for ($counter = 1; $counter <= $lastpage; $counter++) {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
                }
            }
            elseif ($lastpage > 5 + ($display * 2)) { //enough pages to hide some
//close to beginning; only hide later pages
                if ($page < 1 + ($display * 2)) {
                    for ($counter = 1; $counter < 4 + ($display * 2); $counter++) {
                        if ($counter == $page)
                            $pagination.= "<span class=\"current\">$counter</span>";
                        else
                            $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
                    }
                }
//in middle; hide some front and some back
                elseif ($lastpage - ($display * 2) > $page && $page > ($display * 2)) {
                    for ($counter = $page - $display; $counter <= $page + $display; $counter++) {
                        if ($counter == $page)
                            $pagination.= "<span class=\"current\">$counter</span>";
                        else
                            $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
                    }
                }
//close to end; only hide early pages
                else {
                    for ($counter = $lastpage - (2 + ($display * 2)); $counter <= $lastpage; $counter++) {
                        if ($counter == $page)
                            $pagination.= "<span class=\"current\">$counter</span>";
                        else
                            $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
                    }
                }
            }
//next button && last button
            if ($page < $counter - 1){
                $pagination.= "<a href=\"$targetpage?page=$next\">next >></a>";
            }
            else
                $pagination.= "<span class=\"disabled\">next >></span>";
                $pagination.= "<a href=\"$targetpage?page=$lastpage\">Last</a>";
                $pagination.= "</div>\n";
        }
        return $pagination;
    }
}
?>